package com.courier.controller;
import com.courier.entity.User;
import com.courier.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 新增用户
    @PostMapping
    public boolean addUser(@RequestBody User user) {
        return userService.save(user);
    }

    // 更新用户信息
    @PutMapping
    public boolean updateUser(@RequestBody User user) {
        return userService.updateById(user);
    }

    // 删除用户
    @DeleteMapping("/{id}")
    public boolean deleteUser(@PathVariable Long id) {
        return userService.removeById(id);
    }

    // 根据ID查询
    @GetMapping("/{id}")
    public User getById(@PathVariable Long id) {
        return userService.getById(id);
    }

    // 根据用户名查询
    @GetMapping("/username/{username}")
    public User getByUsername(@PathVariable String username) {
        return userService.findByUsername(username);
    }

    // 修改用户状态
    @PutMapping("/status/{id}/{status}")
    public boolean updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        return userService.updateStatus(id, status);
    }

    // 查询所有用户
    @GetMapping
    public List<User> getAll() {
        return userService.list();
    }
}